package net.hzlj.platform.enterprise.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.google.gson.Gson;

import net.hzlj.platform.enterprise.BootApp;
import net.hzlj.platform.enterprise.beans.ui.LocationBean;
import net.hzlj.platform.enterprise.common.Constant;
import net.hzlj.platform.enterprise.receiver.LocationNotification;
import net.hzlj.platform.enterprise.socket.SocketManager;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscription;
import rx.functions.Action1;
import rx.functions.Func1;

public class MainService extends Service {

    private static final String TAG = MainService.class.getSimpleName();

    //重新链接
    private Subscription mReconnectSubscription;
    //心跳
    private Subscription mHeartBeatSubscription;

    public MainService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        android.util.Log.i(TAG, "onCreate");
        sendBroadcast(new Intent(Constant.Intent.ACTION_SOCKET_START));
        sendBroadcast(new Intent(Constant.Intent.ACTION_REFRESH_NOTIFICATION));
        //重新链接
        if (mReconnectSubscription != null){
            mReconnectSubscription.unsubscribe();
        }
        mReconnectSubscription = Observable.interval(0, Constant.Socket.DEFAULT_RECONNECT_TIME, TimeUnit.MILLISECONDS).filter(new Func1<Long, Boolean>() {
            @Override
            public Boolean call(Long aLong) {
                return true;
            }
        }).subscribe(new Action1<Long>() {
            @Override
            public void call(Long aLong) {
                sendBroadcast(new Intent(Constant.Intent.ACTION_REFRESH_NOTIFICATION));
                sendBroadcast(new Intent(Constant.Intent.ACTION_SOCKET_RECONNECT));
            }
        });

        //心跳
        if (mHeartBeatSubscription != null){
            mHeartBeatSubscription.unsubscribe();
            mReconnectSubscription = null;
        }
        mHeartBeatSubscription = Observable.interval(0, Constant.Socket.DEFAULT_HEART_BEAT_TIME, TimeUnit.MILLISECONDS).filter(new Func1<Long, Boolean>() {
            @Override
            public Boolean call(Long aLong) {
                return true;
            }
        }).subscribe(new Action1<Long>() {
            @Override
            public void call(Long aLong) {
                //String header = Constant.Socket.HEART_BEAT_MESSAGE;
                String header = new Gson().toJson(new LocationBean(BootApp.getInstance().getUserTelephone()));
                Log.i(TAG, "DEFAULT_HEART_BEAT_TIME interval:" + aLong + ",header:" + header);
                //刷新通知
                sendBroadcast(new Intent(Constant.Intent.ACTION_REFRESH_NOTIFICATION));
                Intent intent = new Intent(Constant.Intent.ACTION_SOCKET_MESSAGE_SEND);
                intent.putExtra("Message",header);
                sendBroadcast(intent);
            }
        });
    }

    private void refreshNotification() {
        LocationNotification.getInstance().createNotification(this);
        if (null != LocationNotification.getInstance().getNotification()) {
            startForeground(BIND_AUTO_CREATE, LocationNotification.getInstance()
                    .getNotification());
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        android.util.Log.i(TAG, "onBind");
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        android.util.Log.i(TAG, "onStartCommand flags:" + flags + ",startId:" + startId);
        if (intent != null) {
            android.util.Log.i(TAG, "action :" + intent.getAction());
        }
        sendBroadcast(new Intent(Constant.Intent.ACTION_SOCKET_RECONNECT));
        return Service.START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mReconnectSubscription != null) {
            mReconnectSubscription.unsubscribe();
        }
        mReconnectSubscription = null;
        if (mHeartBeatSubscription != null){
            mHeartBeatSubscription.unsubscribe();
        }
        mHeartBeatSubscription = null;
        sendBroadcast(new Intent(Constant.Intent.ACTION_SOCKET_DESTORY));
        startService(new Intent(this, MainService.class));
    }
}
